var api = require('../../../../config/api.js');
var util = require('../../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    integral: 0,
    integralDetailed: [], //积分明细数组
    total: 0, //总共条数
    page: 1, //当前页
    limit: 10, //每页的数量
    userId: ''
  },

  //获取积分明细
  getIntegralAllLog() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      userId: wx.getStorageSync('userid')
    })
    let {
      page,
      limit,
      userId
    } = this.data
    util.request(api.IntegralAllLog + `?type=1&page=${page}&limit=${limit}&userId=${userId}`).then(res => {
      console.log(res);
      if (res.errno == 0) {
        let list = res.data.list.map(x => {
          switch (x.type) {
            case 0:
              x.type = '积分转出'
              break;
            case 1:
              x.type = '系统奖励'
              break;
            case 2:
              x.type = '积分提现'
              break;
            case 3:
              x.type = '后台人工增加积分'
              break;
            case 4:
              x.type = '积分转入'
              break;
            case 5:
              x.type = '积分支付订单'
              break;
            case 10:
              x.type = '后台人工减少积分'
              break;
            default:
              break;
          }
          if(x.yhk){
            x.yhk = x.yhk.slice(-4)
          }
          return x
        })
        wx.hideLoading({
          success: (result) => {
            this.setData({
              total: res.data.total,
              integralDetailed: [...this.data.integralDetailed, ...list]
            })
          },
        })
      } else {
        wx.hideLoading({
          success: () => {
            wx.showToast({
              title: '加载失败',
              icon: 'error'
            })
          }
        })
      }
    }).catch(err => {
      wx.hideLoading({
        success: () => {
          wx.showToast({
            title: '加载失败',
            icon: 'error'
          })
        }
      })
    })
  },

  //页面触底事件
  onReachBottom() {
    if (this.data.page * this.data.limit > this.data.total) {
      wx.showToast({
        title: '到底啦',
        icon: 'none'
      })
    } else {
      this.data.page++
      this.getIntegralAllLog()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    util.request(api.AuthUserInfo).then(res => {
      //获取总积分
      this.setData({
        integral: res.data.integral
      })
    })
    this.getIntegralAllLog()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },



})